home *** CD-ROM | disk | FTP | other *** search
- Path: news.tu-chemnitz.de!news
- From: hfst@hrz.tu-chemnitz.de (Hans Steffani)
- Newsgroups: comp.lang.c
- Subject: Re: Beginner need help??????????????
- Date: 10 Apr 96 15:38:18 GMT
- Organization: University of Technology Chemnitz, FRG
- Message-ID: <4kgkvj$qem@narses.hrz.tu-chemnitz.de>
- References: <4kem82$5j3@dewey.csun.edu>
- NNTP-Posting-Host: click.hrz.tu-chemnitz.de
-
- kc44097@csun.edu (chen) writes:
-
-
-
- >-------- program 1 -------------------------
- >#include <stdio.h>
-
- >float answer;
-
- > answer = 1 / 3;
- The caluclation is done in an integral as both operands are
- of integral type. The result is 0 and casted to 0.0 which
- means 0.
-
- > printf("The value of 1/3 is %f\n",
- > answer);
-
-
-
-
- >float result;
-
- >main()
- >{
- > result = 7.0 / 22.0;
-
- > printf("The result is %d\n", result);
-
- The type of result is float, not integer. The printf() treats
- the bitpattern of result as an integer leads printf() to assume
- that the integer 0 is passed. If you do not like this, use
-
- printf("blah %f\n", result );
-
- h.f.s.
-
- >int integer;
- >float floating;
- > floating = 7.0 / 22.0;
- >
- > integer = floating;
-
- > printf("The value of integer is %f\n", integer);
- >???? The answer should be 0.31818 but compiler give me 0.000000 ?????
-
- Integer cannot hold 0.31 as it is integral type. Further on
- %f does not fit to the type of the argument integer. Change
- to
- printf("The value of integer is %d\n", integer);
-
- h.f.s.
-
- PS:
- Looks like a homework problem but now it's too late.
- h.f.s.
- --
- Hans Friedrich Steffani
- Institut fuer Elektrische Maschinen und Antriebe
- TU Chemnitz-Zwickau
- e-mail: hans.steffani@e-technik.tu-chemnitz.de
-